# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=poppler
pkgver=0.90.1
pkgrel=0
pkgdesc="PDF rendering library based on xpdf 3.0"
url="https://poppler.freedesktop.org/"
arch="all"
options="!check" # No test suite.
license="GPL-2.0-or-later"
depends_dev="cairo-dev glib-dev"
makedepends="$depends_dev cmake fontconfig-dev
	gobject-introspection-dev lcms2-dev libjpeg-turbo-dev
	libpng-dev libxml2-dev openjpeg-dev openjpeg-tools
	tiff-dev zlib-dev"
subpackages="$pkgname-dev $pkgname-doc $pkgname-glib $pkgname-utils"
source="https://poppler.freedesktop.org/poppler-$pkgver.tar.xz"

# secfixes:
#   0.80.0-r0:
#     - CVE-2019-9959

prepare() {
	default_prepare

	local _linked_pkg=poppler-qt5
	local _linked_apkbuild="$startdir"/../../community/$_linked_pkg/APKBUILD
	mkdir -p "$builddir"
	if  [ -f "$_linked_apkbuild" ]; then
		local _linked_ver=$( . "$_linked_apkbuild"; echo "$pkgver")
		if [ "$_linked_ver" != "$pkgver" ]; then
			error "pkgver needs to be identical with $_linked_pkg ($_linked_ver)"
			return 1
		fi
	fi
}

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build \
		-DCMAKE_BUILD_TYPE=None \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DENABLE_UNSTABLE_API_ABI_HEADERS=ON \
		-DBUILD_QT5_TESTS=OFF \
		-DENABLE_QT5=OFF \
		-DCMAKE_CXX_FLAGS="$CXXFLAGS" \
		-DCMAKE_C_FLAGS="$CFLAGS" \
		$CMAKE_CROSSOPTS .
	make -C build
}

package() {
	make -C build DESTDIR="$pkgdir" install
}

utils() {
	pkgdesc="Poppler's xpdf-workalike command line utilities"
	install -d "$subpkgdir"/usr
	mv "$pkgdir"/usr/bin "$subpkgdir"/usr/
}

glib() {
	pkgdesc="Glib wrapper for poppler"
	replaces="poppler-gtk"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libpoppler-glib.so.* \
		"$pkgdir"/usr/lib/girepository* \
		"$subpkgdir"/usr/lib/
}

_cpp() {
	pkgdesc="Pure C++ wrapper for poppler"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libpoppler-cpp.so.* \
		"$subpkgdir"/usr/lib/
}

sha512sums="c0a38c3c40196f8b847f0737152d0cbb40656870a8ceb766134ee22ec7c21bc979a13f56b7596a13ae2f08f1a9a2c396ba7f5ff176d02c262af7e863c2afde34  poppler-0.90.1.tar.xz"
